package com.zhcx.zhcxlibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import java.util.Iterator;
import java.util.List;
import java.util.Stack;

/**
 * ProjectName：BranchWisdomBus
 * PackageName：com.zhcx.wisdombus.utils
 * description:页面管理
 * Date：2016/9/12 17:59
 * Author：Steven
 * ClassName:AppManager
 **/
public class AppManager {
    public static Stack<Activity> activityStack;
    private static AppManager instance;

    /**
     * 单一实例
     */
    public static AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    public static void sendBroadcastReceiver(Context mContext){
        Intent i = new Intent();
        i.setAction("action_logins");
        mContext.sendBroadcast(i);
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        if(null != activityStack && activityStack.size() != 0){
            Activity activity = activityStack.lastElement();
            return activity;
        }else{
            return null;
        }
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            Stack<Activity> mActivity = new Stack<>();
            mActivity.addAll(activityStack);
            activityStack.clear();
            mActivity.remove(activity);
            activityStack.addAll(mActivity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());

        } catch (Exception e) {
        }
    }

    /**
     * 结束指定的Activity
     */
    public void getActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 页面关闭  移除activity集合中的值
     */
    public void removeActivity(Activity activity){
        if (activity != null) {
            Stack<Activity> mActivity = new Stack<>();
            mActivity.addAll(activityStack);
            activityStack.clear();
            if (mActivity.size() > 0){
                for (Activity mActivityBean: mActivity){
                    if (mActivityBean == activity){
                        mActivity.remove(activity);
                    }
                }
            }
            activityStack.addAll(mActivity);
        }
    }

    /**
     * 得到指定类名的Activity
     */
    public Activity getActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                return activity;
            }
        }
        return null;
    }

    /**
     * 判断当前类是否已经存在
     *
     * @param paramClass
     * @return
     */
    public boolean queryActivity(Class<?> paramClass) {
        if (this.activityStack != null) {
            Iterator localIterator = this.activityStack.iterator();
            while (localIterator.hasNext())
                if (((Activity) localIterator.next()).getClass().equals(paramClass))
                    return true;
        }
        return false;
    }

    public void finishAllActivityExceptLast() {
        while (true) {
            int j;
            try {
                if (this.activityStack == null)
                    continue;
                int i = this.activityStack.size();
                j = 0;
                if (j >= i)
                    continue;
                if ((this.activityStack.get(j) != null) && (j != i - 1)) {
                    ((Activity) this.activityStack.get(j)).finish();
                    this.activityStack.clear();
                    this.activityStack = null;
                    return;
                }
            } catch (Exception localException) {
                localException.printStackTrace();
                return;
            }
            j++;
        }
    }
}
